// SettlementServiceImpl.java
package com.cddx.logistics.service.impl;

import com.cddx.logistics.domain.dto.SettlementQueryDTO;
import com.cddx.logistics.domain.vo.SettlementVO;
import com.cddx.logistics.mapper.SettlementMapper;
import com.cddx.logistics.service.SettlementService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service
public class SettlementServiceImpl implements SettlementService {

    @Resource
    private SettlementMapper settlementMapper;

    @Override
    public PageInfo<SettlementVO> queryPage(SettlementQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(), queryDTO.getLimit());
        List<SettlementVO> list = settlementMapper.querySettlements(queryDTO);
        return new PageInfo<>(list);
    }

    @Override
    public boolean updateSettlementState(Integer id, Integer settlementState) {
        int result = settlementMapper.updateSettlementState(id, settlementState);
        return result > 0;
    }

    @Override
    public SettlementVO getById(Integer id) {
        SettlementQueryDTO queryDTO = new SettlementQueryDTO();
        queryDTO.setId(id); // 需要先在SettlementQueryDTO中添加id字段
        List<SettlementVO> list = settlementMapper.querySettlements(queryDTO);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public boolean updateState(SettlementVO settlementVO) {
        try {
            // 添加日志记录
            log.info("更新项目状态: ID={}, 完成状态={}, 结算状态={}",
                    settlementVO.getId(),
                    settlementVO.getState(),
                    settlementVO.getSettlementState());

            // 执行更新操作
            return settlementMapper.updateState(settlementVO) > 0;
        } catch (Exception e) {
            log.error("更新状态失败", e);
            throw new RuntimeException("更新状态失败");
        }
    }

    @Override
    public boolean deleteById(Integer id) {
        try {
            int result = settlementMapper.deleteById(id);
            return result > 0;
        } catch (Exception e) {
            log.error("删除结算记录失败", e);
            throw new RuntimeException("删除结算记录失败");
        }
    }
}
